﻿#region File Header
//-----------------------------------------------------------------------------
// SibLib
//
// Copyright (C) 2010 Julien Villers
// This program is distributed under the terms of the 
// GNU Lesser General Public License (LGPL).
// See Docs/lgpl.txt and Docs/gpl.txt
//-----------------------------------------------------------------------------
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace SibLib.GameState
{
    /// <summary>
    /// A GameScreen made to display a scrolling text.
    /// Can be used for credits for instance.
    /// </summary>
    public class ScrollingTextScreen : GameScreen
    {
        #region Members
        private float m_Position;
        private bool m_IsOver;
        #endregion

        #region Properties
        public List<string> Texts
        {
            get;
            private set;
        }

        public float ScrollingSpeed
        {
            get;
            set;
        }

        public int LineSpacing
        {
            get;
            set;
        }

        public Color BaseColor
        {
            get;
            set;
        }

        public Color ShadeColor
        {
            get;
            set;
        }
        #endregion

        public ScrollingTextScreen() :
            base()
        {
            Texts = new List<string>(64);

            m_Position = 740.0f;
            m_IsOver = false;

            ScrollingSpeed = 2.0f;
            LineSpacing = 42;

            BaseColor = Color.White;
            ShadeColor = Color.Black;
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);

            if (coveredByOtherScreen)
                return;

            m_Position -= ScrollingSpeed * (gameTime.ElapsedGameTime.Milliseconds / 100.0f);

            // TODO: looping, auto-finish, whatever, do something when nothing on screen anymore.
        }

        public override void HandleInput(GameTime gameTime, InputState input)
        {
            base.HandleInput(gameTime, input);

            PlayerIndex idx;
            if (input.IsMenuSelect(ControllingPlayer, out idx) || input.IsMenuCancel(ControllingPlayer, out idx))
            {
                //SoundManager.Play(Constants.CUE_MENU_CANCEL);         // TODO: I want my sound!
                ExitScreen();
                return;
            }

            if (m_IsOver)
            {
                ExitScreen();
                return;
            }

            if (input.CurrentGamePadStates[(int)ControllingPlayer].DPad.Down == ButtonState.Pressed ||
                input.CurrentGamePadStates[(int)ControllingPlayer].ThumbSticks.Left.Y < -0.1f)
            {
                m_Position -= ScrollingSpeed * 0.16f * 2.0f;
            }
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);

            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
            spriteBatch.Begin();
            SpriteFont font = ScreenManager.Font;
            Vector2 position = new Vector2(128.0f, m_Position);
            foreach (string str in Texts)
            {
                spriteBatch.DrawString(font, str, position + Vector2.One, ShadeColor);
                spriteBatch.DrawString(font, str, position, BaseColor);
                position.Y += LineSpacing;
            }
            spriteBatch.End();

            if (position.Y < -LineSpacing)
                m_IsOver = true;
        }
    }
}
